<?php
namespace CountLike\Controller;
use Common\Controller\CountLikeController;
class ActivityController extends CountLikeController {


    public function __construct()
    {
        $sessionid = 'PHPSESSID='.$this->getSessionId();

        $getsessionid = $_SERVER['HTTP_COOKIE'];
        if ($sessionid!=$getsessionid){
            $this->returnData('','状态已过期请重新授权',400);
        }
    }

    /**
     *  获取基本信息
     */
    public function getinfo(){

        //规则
        $data['rule'] = array('完成挑战即可获得随机红包','累计金额达到5元可以发起提现。','提现请添加工作人员微信ONETECHCLUB，工作人员将定时为您发放红包奖励。','放假期间回复可能有延误，我们工作人员看到消息会第一时间为您发放红包，请见谅！','有其他咨询事宜，请添加我们工作人员微信。微信号：ONETECHCLUB。');


        $this->returnData($data);

    }


    /**
     * 挑战
     */
    public function change(){

        $member_id = I('post.member_id','','int');

        if (!$member_id){
            $this->returnData('','参数错误',401);
        }

        $date = date('Y-m-d',time());

        $where['countlike_member_id'] = $member_id;
        $where['signin_time'] = $date;

        $issign="";
        $issign = M('countlike_signin')->where($where)->getField('signin_num'); //判断今天有没有签到
        if (!$issign||$issign==""){
            $data = array(
                'countlike_member_id'=>$member_id,
                'signin_time'=>$date,
                'signin_num'=>'1'
            );

            M('countlike_signin')->data($data)->add();

            M('countlike_member')->where(array('countlike_member_id'=>$member_id))->save(array('giveopportunity'=>'1')); //每天赠送机会
        }

        $opportunity = M('countlike_member')->field('opportunity,giveopportunity,nickname,avatarurl')->where(array('countlike_member_id'=>$member_id))->find();//获取挑战机会

        $chang = $opportunity['opportunity']+$opportunity['giveopportunity'];

        if ($chang>0){
            if ($opportunity['giveopportunity']>0){
                M('countlike_member')->where(array('countlike_member_id'=>$member_id))->setDec('giveopportunity'); //先减去赠送的
            }else{
                M('countlike_member')->where(array('countlike_member_id'=>$member_id))->setDec('opportunity'); //挑战机会减一
            }

            $insert = array(
                'countlike_member_id'=>$member_id,
                'num'=>0,
                'date'=>$date,
                'week'=>(int)date('W'),
                'year'=>(int)date('Y'),
                'nickname'=>$opportunity['nickname'],
                'avatarurl'=>$opportunity['avatarurl'],
                'createtime'=>date('Y-m-d H:i:s',time())
            );
           $id = M('countlike_change')->data($insert)->add();

           $wherech['countlike_member_id'] = $member_id;
            $wherech['date'] = $date;
           $record = M('countlike_change')->where($wherech)->max('num');

           $isshow = 1; //分享复活
            $res['set'] = array(
                'opportunity'=>$chang,
                'time'=>120,//挑战时间
                'record'=>$record,
                'chang_id'=>$id,
                'isshow'=>$isshow,
            );
            $res['question'] = $this->activeityquestion();
            $this->returnData($res,'成功',200);
        }

        $this->returnData(array('opportunity'=>$chang),'挑战机会已用完,分享到群获得更多机会',101);

    }


    /**
     * 答对一题
     */
    public function inanswer(){

        $member_id = I('post.member_id','','int');
        $chang_id = I('post.chang_id','','int');
        $where['anwser'] = I('post.anwser','','htmlspecialchars');
        $where['countlike_questions_id'] = I('post.questions_id','','int');
        $state = I('post.state','','int');


        if ($state=='2'&&$member_id){  //答错返回
            $wherech['countlike_member_id'] = $member_id;
            $wherech['date'] = date('Y-m-d',time());
            $record = M('countlike_change')->where($wherech)->max('num');

            $this->returnData(array('record'=>$record));

        }

        if (!$member_id||!$chang_id||!$where['anwser']||!$where['countlike_questions_id']){
            $this->returnData('','参数错误',401);
        }


        $id = M('countlike_questions')->where($where)->getField('countlike_questions_id');

        if (empty($id)){
            $this->returnData('','答错了',100);
        }

        M('countlike_change')->where(array('id'=>$chang_id))->setInc('num');

        $wherech['countlike_member_id'] = $member_id;
        $wherech['date'] = date('Y-m-d');
        $record = M('countlike_change')->where($wherech)->max('num');

        $this->returnData(array('record'=>$record));
    }

    /**
     * 微信群分享记录
     */
    public function sharerecord(){

        $member_id = I('post.member_id','','int');
        $shareType = I('post.shareType','2','int');
        $encryptedData = $_POST['encryptedData'];
        $iv = $_POST['iv'];


        if (!$encryptedData||!$member_id||!$iv){
            $this->returnData('','参数错误',401);
        }

        $info = M('countlike_member')->field('appid,session_key')->where(array('countlike_member_id'=>$member_id))->find();

        $appid = $info['appid'];
        $sessionkey = $info['session_key'];

        $openGId = $this->getopenGId($appid,$sessionkey,$encryptedData,$iv);

        if ($openGId=='-1'||empty($openGId)){
            $this->returnData('','',100);
        }

        $time = date('Y-m-d',time());

        $where['sharetime'] = $time;
        $where['countlike_member_id'] = $member_id;
        $where['opengid'] = $openGId;

        if ($shareType=='1'){
            $interior = $record = M('countlike_activity_interior')->where($where)->getField('id'); //判断是否已分享
            if ($interior){
                M('countlike_activity_interior')->where($where)->setInc('sharenum');
                $this->returnData(array('repetition'=>'yes'),'今天已分享',201);
            }
            //如果没分享过则插入
            $data = array(
                'opengid'=>$openGId,
                'sharetime'=>$time,
                'sharenum'=>1,
                'countlike_member_id'=>$member_id,
            );

            M('countlike_activity_interior')->data($data)->add();
            $this->returnData(array('repetition'=>'repetition'),'立即复活',200);
        }


        $where2['sharetime'] = $time;
        $where2['countlike_member_id'] = $member_id;

        $count = M('countlike_activity_group')->where($where2)->count();

        $maxshare = 10;

        if ($count>=$maxshare){
            $this->returnData(array('repetition'=>'out'),'一天有效分享次数为10',201);
        }


        $record = "";
        $record = M('countlike_activity_group')->where($where)->getField('sharenum'); //判断是否已分享

        //如果已分享则自增分享次数然后返回
        if ($record){
            M('countlike_activity_group')->where($where)->setInc('sharenum');

            $this->returnData(array('repetition'=>'yes'),'今天已分享',201);
        }


        //如果没分享过则插入，用户挑战机会加一
        $data = array(
            'opengid'=>$openGId,
            'sharetime'=>$time,
            'sharenum'=>1,
            'countlike_member_id'=>$member_id,
        );

        M('countlike_activity_group')->data($data)->add();

        M('countlike_member')->where(array('countlike_member_id'=>$member_id))->setInc('opportunity'); //挑战机会加一

        $opportunity = M('countlike_member')->field('opportunity,giveopportunity')->where(array('countlike_member_id'=>$member_id))->find();//获取挑战机会

        $chang = $opportunity['opportunity']+$opportunity['giveopportunity'];

        $this->returnData(array('repetition'=>'no','opportunity'=>$chang),'分享成功',200);
    }

    /**
     * 更多好玩
     */
    public function moreplay(){

        $data = array(
            'appid'=>'wx6b1c7907c5733fc9',
            'url'=>'pages/index/index'
        );
            $this->returnData($data);
    }

    /**
     * 排行
     */
    public function rank(){

        $state = I('post.state','','int');
        if ($state=='1'){
            $date = date('Y-m-d');
            $rank = $this->getrank($date,$state);
            $tabName_1 = '今日排行';
            $tabName_2 = '端午得奖公示';
            $msg['tabName_1'] = $tabName_1;
            $msg['tabName_2'] = $tabName_2;
            $this->returnData($rank,$msg);
        }

        if ($state=='2'){
            $rank = $this->gift();
            $tabName_2 = '端午得奖公示';
            $this->returnData($rank,$tabName_2);
        }



    }

    /**
     * 练习模式
     */
    public function practice(){

        $question[] = $this->get_question(2,15);
        $question[] = $this->get_question(5,15);
        $question[] = $this->get_question(8,10);

       //$question[] = $this->get_question(2,5);

        foreach ($question as $key=>$val){

            foreach ($val as $va1){
                $arr[] = $va1;
            }
        }

        $this->returnData($arr,6); // 6是答题时间
    }

    /**
     * 分享模板
     */
    public function sharetem(){

        $data = array(
            'title'=>'答题有奖',
            'imgUrl'=>$this->url.'share/susuanshare.jpg',
            'desc'=>'锻炼你的大脑'
        );

        $this->returnData($data);

    }

}